package com.sy.core.web.system;

import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.sy.core.util.BaseUUID;
import com.sy.core.util.RestResult;
import com.sy.core.web.BaseCtl;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.ResponseBody;


import com.sy.core.model.SysMenu;
import com.sy.core.service.SysMenuService;




/**
 * 	TODO 请用一句话描述该类的作用
 * @author CaiXinChong email:1060296473@qq.com
 * @version 1.0
 * @since 1.0
 */

@Controller
@RequestMapping("/sysMenu")
public class SysMenuCtl extends BaseCtl {

    @Autowired
    private SysMenuService  sysMenuService;
    

	@RequestMapping("/{path}")
	public ModelAndView toAnyPage(@PathVariable String path,ModelAndView mv){
	    mv.setViewName(path);
	    return mv;
    }
    
    /**
     * Action for Add
     */
    @RequiresPermissions("/sysMenu/save")
    @RequestMapping(value = "/save",produces = "application/json;charset=utf-8")
    @ResponseBody
    public RestResult save(SysMenu sysMenu,String str){
        RestResult rr = null;
        sysMenu= JSONObject.parseObject(str,SysMenu.class);
        String pk = sysMenu.getPk();
        try {
            if(pk==null||"".equals(pk)){
                pk = BaseUUID.getUUID();
                sysMenu.setPk(pk);
                sysMenuService.insert(sysMenu);
            }else{
                sysMenuService.update(sysMenu);
            }
            rr=new RestResult("200","SUCCESS",pk);
        } catch (Exception e) {
            e.printStackTrace();
            rr = new RestResult("201","FAILED");
        }
        return rr;
    }

    /**
     * Action for update
     */
    @RequestMapping(value = "/update",produces = "application/json;charset=utf-8")
    @ResponseBody
    public RestResult update(SysMenu sysMenu){
        RestResult rr = null;
        try {
        sysMenuService.update(sysMenu);
        rr=new RestResult("200","SUCCESS");
        } catch (Exception e) {
        e.printStackTrace();
        rr = new RestResult("201","FAILED");
        }
        return rr;
    }
    
    /**
     * Action for SelectOneByKey
     */
    @RequestMapping(value = "/query/{key}",produces = "application/json;charset=utf-8")
    @ResponseBody
    public RestResult queryByPrimaryKey(@PathVariable String key){
       try{
            SysMenu sysMenu = sysMenuService.queryByPrimaryKey(key);
      	 	return new RestResult("200","SUCCESS",sysMenu);
       }catch(Exception e){
    	   	logger.error("Query Model SysMenu By PrimaryKey Is Fail:"+e.getMessage());
       }
       		return new RestResult("201","FAIL");
    }
    
    /**
     * Action for SelectList
     */
    @RequestMapping(value = "/list/{pcode}",produces = "application/json;charset=utf-8")
    @ResponseBody
    public RestResult queryList(SysMenu sysMenu,@PathVariable String pcode){
        sysMenu.setPcode(pcode);
        SysMenu sysMenu2 = new SysMenu();
       try{
      		List<SysMenu> sysMenuList = sysMenuService.queryByCondition(sysMenu);
           for (int i = 0; i <sysMenuList.size() ; i++) {
               sysMenu2.setPcode(sysMenuList.get(i).getPk());
               List<SysMenu> sysMenus = sysMenuService.queryByCondition(sysMenu2);
               if(sysMenus!=null&&sysMenus.size()>0){
                   sysMenuList.get(i).setHasChildren("true");
               }
           }
      	 	return new RestResult("200","SUCCESS",sysMenuList);
       }catch(Exception e){
    	   logger.error("Query Model SysMenu List Fail :"+e.getMessage());
    	   return new RestResult("201","FAIL");
       }
    }
    
    /**
     * Action for DeleteModel
     */
    @RequiresPermissions("/sysMenu/delete")
    @RequestMapping("/delete/{pk}")
    @ResponseBody
    public RestResult delete(@PathVariable String pk){
        RestResult rr = null;
        try {
        sysMenuService.delete(pk);
            rr=new RestResult("200","SUCCESS");
        } catch (Exception e) {
            e.printStackTrace();
            rr = new RestResult("201","FAILED");
        }
        return rr;
    }

   
}
